import React, { Component } from 'react';
import { Flex, ListView, Toast, WingBlank, Button } from "antd-mobile"
import { Login } from "dcloud-mobile"
import Link from 'umi/link'
import Router from "umi/router";
import { connect } from "dva/index";
import { loginServer } from "dcloud-utils";
import styles from './index.less';
import { httpUtil } from "dcloud-utils";
import { httpUrl } from "dcloud-core";


class User extends Component {
    constructor(props) {
        super(props);
        // this.state = {
        // }
        this.state = {
            search: true,
            data:
                [
                    //     {
                    //     "appointmentNo": "",
                    //     "appointmentNoNumber": 5,
                    //     "date": "",
                    //     "identityNo": "",
                    //     "isFailed": 0,
                    //     "mobilePhone": "",
                    //     "name": "",
                    //     "selfSubmitAddress": "",
                    //     "selfSubmitId": "",
                    //     "selfSubmitPoint": "",
                    //     "tenantId": "",
                    //     "userId": ""
                    // }
                ]
        }

    }

    getDate = (time) => {
        //获取年份
        var year = time.getFullYear();
        //获取月份
        var month = time.getMonth() + 1;
        //获取日期
        var day = time.getDate();

        //日期小时分钟秒小于10，前面加0
        month = month < 10 ? "0" + month : month;
        day = day < 10 ? "0" + day : day;
        return year + "-" + month + "-" + day;
    }

    getTime = (time) => {
        //获取时间小时
        var hour = time.getHours();
        //获取分钟
        var minutes = time.getMinutes();
        //获取秒
        var seconds = time.getSeconds();

        hour = hour < 10 ? "0" + hour : hour;
        seconds = seconds < 10 ? "0" + seconds : seconds;
        return hour + ":" + minutes + ":" + seconds;
    }

    countDown = (time) => {
        let s = 0;
        let hour = time.split(':')[0];
        let min = time.split(':')[1];
        let sec = time.split(':')[2];
        s = Number(hour * 3600) + Number(min * 60) + Number(sec);
        return s;
    }

    componentDidMount() {
        httpUtil.get(httpUrl.getDetail, { tenantId: localStorage.getItem('tenantId') }).then((res) => {
            if (res.code == 200) {
                this.setState({
                    data: res.data
                })
            }
        }).catch(error => {
            Toast.fail('系统错误', 2);
        })

        // 判断是否在预约时间段
        let appointmentStartTime = localStorage.getItem("appointmentStartTime");
        appointmentStartTime = this.countDown(appointmentStartTime);
        let appointmentEndTime = localStorage.getItem("appointmentEndTime");
        appointmentEndTime = this.countDown(appointmentEndTime);
        let startDate = localStorage.getItem("startDate");
        startDate = Date.parse(startDate);
        let endDate = localStorage.getItem("endDate");
        endDate = Date.parse(endDate);
        let day = new Date();
        let myDay = this.getDate(day);
        myDay = Date.parse(myDay);
        // 先判断日期是否在预约时间内
        if (myDay >= startDate && myDay <= endDate) {
            // 再判断时间是否在预约时间内
            let myTime = new Date();
            myTime = this.getTime(myTime);
            myTime = this.countDown(myTime);
            if (myTime >= appointmentStartTime && myTime <= appointmentEndTime) {
                // 在预约时间范围内
                this.setState({
                    search: false
                })
            }
        }
    }

    render() {
        let reDom = null;
        if (this.state.data.length > 0) {
            reDom = <div>
                <WingBlank>
                    {
                        this.state.data.map((item) => {
                            return <div className={styles.box}>
                                <p className={styles.list}>
                                    <span className={styles.title}>预约状态：</span>
                                    <span className={styles.text}>{item.isFailed === 0 ? '预约成功' : '预约失败'}</span>
                                </p>
                                <p className={styles.list}>
                                    <span className={styles.title}>预约号：</span>
                                    <span className={styles.text}>{item.appointmentNo}</span>
                                </p>
                                <p className={styles.list}>
                                    <span className={styles.title}>预约口罩数量：</span>
                                    <span className={styles.text}>{item.maskAmount}</span>
                                </p>
                                <p className={styles.list}>
                                    <span className={styles.title}>姓名：</span>
                                    <span className={styles.text}>{item.name}</span>
                                </p>
                                <p className={styles.list}>
                                    <span className={styles.title}>手机号：</span>
                                    <span className={styles.text}>{item.mobilePhone}</span>
                                </p>
                                <p className={styles.list}>
                                    <span className={styles.title}>身份证号：</span>
                                    <span className={styles.text}>{item.identityNo}</span>
                                </p>
                                <p className={styles.list}>
                                    <span className={styles.title}>地址：</span>
                                    <span className={styles.text}>{item.address ? item.address : '-'}</span>
                                </p>
                                <p className={styles.list}>
                                    <span className={styles.title}>自提点：</span>
                                    <span className={styles.text}>{item.selfSubmitPoint}</span>
                                </p>
                                <p className={styles.list}>
                                    <span className={styles.title}>自提点地址：</span>
                                    <span className={styles.text}>{item.selfSubmitAddress}</span>
                                </p>
                                <p className={styles.list}>
                                    <span className={styles.title}>预约时间：</span>
                                    <span className={styles.text}>{item.date}</span>
                                </p>
                            </div>
                        })
                    }
                </WingBlank>
                <p className={styles.p} style={{ color: 'rgba(0,0,0,0.4)' }}>预约系统订单量较大，系统刷新可能出现延迟</p>
            </div>
        } else {
            reDom = <div className={styles.img}>
                <img src={require('../../../assets/images/noData.png')}></img>
                <p className={styles.p}>今日暂无预约</p>
                <p className={styles.p} style={{ color: 'rgba(0,0,0,0.4)' }}>预约系统订单量较大，系统刷新可能出现延迟</p>
            </div>
        }
        return <div className={styles.user}>
            {reDom}
            {
                this.state.search ?
                    <Button onClick={() => {
                        Router.push('/history');
                    }} className={styles.button}>历史预约记录</Button> : null
            }
        </div>
    }
}

export default User;